package us.gaaoc.framework.webapp.backingbeans.judge;

import java.util.List;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;

import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.util.DataPage;
import us.gaaoc.framework.model.util.PagedListDataModel;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 * @author Sukhada
 * @date Jul 5, 2006
 * @version GAJE_BibbCounty_1.0
 * 
 * <!--
 * 		Edited by Brandon Denney
 * 		2008/06/16
 * 
 * 		Updated the page to have a dynamic table, and updated features to
 * 		make the page compatible with GAJE 2.0
 * -->
 * 
 * The backing bean for the judge dashboard.
 */
public class MainPage extends BaseBackingBean {
	
	/* **************** *
	 * Global Variables *
	 * **************** */	
	
	/** Serial Version UID */
	private static final long serialVersionUID = 981745L;
	/** The data model that holds the information for the data
	 *  table being displayed. */
    private DataModel tableDataModel;
    /** The data model that holds the information for the second data table
     * that is displayed on selecting Child Enforcement Main */
    private DataModel secondTableDataModel;
	/** A place-holder for the personal case filter. */
	private static HtmlSelectBooleanCheckbox personalCases;
    /** The current data table being displayed.  This is an internal variable
     * 	name. */
    private static String displayTable;
    /** Indicates which column the data table is to be sorted by. */
    private static String columnOrder;
	/** Indicates whether the sorted column is to be displayed in ascending
	 *  or descending order. */
	private static String listOrdering;
	/** A number to fill in the SQL query for the main data table dynamically. */
	private static int statusCode1_1;
	/** A number to fill in the SQL query for the main data table dynamically. */
	private static int statusCode1_2;
	/** A number to fill in the SQL query for the main data table dynamically. */
	private static int statusCode1_3;
	/** A number to fill in the SQL query for the  main data table dynamically. */
	private static int statusCode1_4;
	/** A place-holder for the case details link UI element. */
	private UIParameter caseDetailsPageClicked2;
	/** A flag indicating if the second table is shown or not. */
	private static boolean displaySecondTable;
	/** A number to fill in the SQL query for the second data table dynamically. */
	private static int statusCode2_1;
	/** A number to fill in the SQL query for the second data table dynamically. */
	private static int statusCode2_2;
	/** A number to fill in the SQL query for the second data table dynamically. */
	private static int statusCode2_3;
	/** A number to fill in the SQL query for the second data table dynamically. */
	private static int statusCode2_4;
	/** A variable to hold a single case to be worked on. */
	/** An integer representing the number of rows retrieved from the SQL query
	 *  that populates the data table. */
	private int filingsRowCount;
	/** A flag variable indicating whether the sorted column is to be
	 *  put in descending order or not. */
	private static boolean desc;
	/** A flag variable indicating if the displayed table is ordered
	 *  by the initiated column. */
	private static boolean orderByInstantiation;
	/** A flag variable indicating if the displayed table is ordered
	 *  by the last event time column. */
	private static boolean orderByLastEvent;
	/** A flag indicating if the cases should be filtered to only cases the
	 *  user has worked on previously. */
	private static String personalFilter;
	/** The title of the current table being displayed. Used as a backing 
	 *  element for displaying the correct title for the shown data table. */
	private static String currentTableName;
	/** Copy of the current session map. */
	@SuppressWarnings("unchecked")
	private Map sessionMap;
	
	private int transmissionErrorCount;
	private Filing filing;
	private UIParameter caseDetailsPageClicked;
	
	private String courtUuid;
	
	/* ******* *
	 * Getters *
	 * ******* */
	
	/**
	 * Getter for the tableDataModel data model.
	 * 
	 * @return the data model containing the requested case listing.
	 */
	public DataModel getTableDataModel() {
		tableDataModel = new MainTableDataModel(10);
		return tableDataModel;
	}
	
	/**
	 * Getter for the secondTableDataModel data model.
	 * 
	 * @return the data model containing the requested case listing for the
	 * second table.
	 */	
	public DataModel getSecondTableDataModel() {
		secondTableDataModel = new SecondaryTableDataModel(10);
		return secondTableDataModel;
	}
	
	/**
	 * Getter for the personalCases html checkbox place-holder.
	 * 
	 * @return the place-holder for the personalCases checkbox.
	 */
	public HtmlSelectBooleanCheckbox getPersonalCases() {
		return personalCases;
	}
	
	/**
	 * Getter for the displayTable String.
	 * 
	 * @return the internal variable name for the displayed table.
	 */
	public String getDisplayTable() {
		return displayTable;
	}
	
	/**
	 * Getter for the columnOrder flag.
	 * 
	 * @return a flag indicating which column the data table is to
	 * be sorted by.
	 */
	public String getColumnOrder() {
		return columnOrder;
	}
	
	/**
	 * Getter for the listOrdering flag.
	 * 
	 * @return a flag indicating if the sorted column is displayed
	 * in ascending or descending order.
	 */
	public String getListOrdering() {
		return listOrdering;
	}
	
	/**
	 * Getter for the statusCode1_1 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the main data
	 * table.
	 */
	public int getStatusCode1_1() {
		return statusCode1_1;
	}
	
	/**
	 * Getter for the statusCode1_2 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the main data
	 * table.
	 */
	public int getStatusCode1_2() {
		return statusCode1_2;
	}
	
	/**
	 * Getter for the statusCode1_3 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the main data
	 * table.
	 */
	public int getStatusCode1_3() {
		return statusCode1_3;
	}
	
	/**
	 * Getter for the statusCode1_4 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the main data
	 * table.
	 */
	public int getStatusCode1_4() {
		return statusCode1_4;
	}
	
	/**
	 * Getter for the statusCode2_1 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the second data
	 * table.
	 */
	public int getStatusCode2_1() {
		return statusCode2_1;
	}
	
	/**
	 * Getter for the statusCode2_2 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the second data
	 * table.
	 */
	public int getStatusCode2_2() {
		return statusCode2_2;
	}
	
	/**
	 * Getter for the statusCode2_3 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the second data
	 * table.
	 */
	public int getStatusCode2_3() {
		return statusCode2_3;
	}
	
	/**
	 * Getter for the statusCode2_4 number.
	 * 
	 * @return a number to fill in the dynamic SQL query for the main second
	 * table.
	 */
	public int getStatusCode2_4() {
		return statusCode2_4;
	}
	
	/**
	 * Getter for the caseDetailsPageClicked2 UI element.
	 * 
	 * @return the case details link UI element.
	 */
	public UIParameter getCaseDetailsPageClicked2() {
		return caseDetailsPageClicked2;
	}
	
	/**
	 * Getter for the filingsRowCount integer.
	 * 
	 * @return the number of entries of the data table that is about to be displayed.
	 */
	public int getFilingsRowCount() {
		
		if(displayTable.equalsIgnoreCase("civil")) {
			List<Filing> rFilings = this.getModelFacade().findCaseFilingsByType(2, courtUuid);
			filingsRowCount = rFilings.size();
		}
		else if(displayTable.equalsIgnoreCase("appellate")) {
			List<Filing> rFilings = this.getModelFacade().findCaseFilingsByType(3, courtUuid);
			filingsRowCount = rFilings.size();
		}
		else {
			List<Filing> rFilings = this.getModelFacade().sortFiling
				(1, getStatusCode1_1(), getStatusCode1_2(), getStatusCode1_3(),
						getStatusCode1_4(), getDesc(),getOrderByLastEvent(),
						getPersonalFilter(), courtUuid);
			
			filingsRowCount = rFilings.size();
		}
		return filingsRowCount;
	}
	
	/**
	 * Getter for the receivedFilingsRowCount integer.
	 * 
	 * @return the number of entries of the data table that is about to be displayed.
	 */
	public int getSecondTableRowCount() {
		
		int receivedFilingsRowCount = 0;

		List<Filing> rFilings = this.getModelFacade().sortFiling
		(1, getStatusCode2_1(), getStatusCode2_2(), getStatusCode2_3(),
			getStatusCode2_4(), getDesc(),getOrderByLastEvent(),
				getPersonalFilter(), courtUuid);
			
		receivedFilingsRowCount = rFilings.size();
			
		return receivedFilingsRowCount;
	}
	
	/**
	 * Getter for the desc flag.
	 * 
	 * @return a flag indicating if the sorted column is to be put in
	 * descending order.
	 */
	public boolean getDesc() {
		return desc;
	}
	
	/**
	 * Getter for the orderByInstantiation flag.
	 * 
	 * @return a flag indicating whether the table is sorted by the
	 * 'Initiated Date' column.
	 */
	public boolean getOrderByInstantiation() {
		return orderByInstantiation; 
	}
	
	/**
	 * Getter for the orderByLastEvent flag.
	 * 
	 * @return a flag indicating whether the table is sorted by the
	 * 'Last Event Time' column.
	 */
	public boolean getOrderByLastEvent() {
		return orderByLastEvent;
	}
	
	/**
	 * Getter for the personalFilter flag.
	 * 
	 * @return a flag indicating if the the tables should be filtered to show
	 * only what cases the user has previously worked on.
	 */
	public String getPersonalFilter() {
		return personalFilter;
	}
	
	/**
	 * Getter for the currentTableName String.
	 * 
	 * @return the name of the table being displayed.
	 */
	public String getCurrentTableName() {
		return currentTableName;
	}
	
	/**
	 * Getter for the sessionMap map.
	 * 
	 * @return the current session map.
	 */
	@SuppressWarnings("unchecked")
	public Map getSessionMap() {
		this.sessionMap = this.getFacesContext().getExternalContext()
			.getSessionMap();
		
		return this.sessionMap;
	}
	
	/**
	 * Setter for the caseDetailsPageClicked2 UI element.
	 * 
	 * @param caseDetailsPageClicked the UI element for the case details link.
	 */
	public void setCaseDetailsPageClicked2(UIParameter caseDetailsPageClicked2) {
		this.caseDetailsPageClicked2 = caseDetailsPageClicked2;
	}
	
	/**
	 * Getter for the displaySecondTable flag.
	 * 
	 * @return a flag indicating if the second table will be
	 * rendered or not.
	 */
	public boolean getDisplaySecondTable() {
		return displaySecondTable;
	}
	
	/**
	 * Getter for the filing object.
	 * 
	 * @return the current filing being worked on.
	 */
	public Filing getFiling() {
		return filing;
	}
	
	/**
	 * Getter for the transmissionErrorCount integer.
	 * 
	 * @return the number of transmission errors.
	 */
	public int getTransmissionErrorCount() {
		return transmissionErrorCount;
	}

	/**
	 * Getter for the caseDetailsPageClicked UI element.
	 * 
	 * @return the UIParameter that is bound to the case details link.
	 */
	public UIParameter getCaseDetailsPageClicked() {
		return caseDetailsPageClicked;
	}
	
	/* ******* *
	 * Setters *
	 * ******* */
	
	/**
	 * Setter for the tableDataModel data model.
	 * 
	 * @param received a data model representing the cases to be shown
	 * in the data table. 
	 */
	public void setTableDataModel(DataModel tableDataModel) {
		this.tableDataModel = tableDataModel;
	}
	
	/**
	 * Setter for the secondTableDataModel data model.
	 * 
	 * @param received a data model representing the cases to be shown
	 * in the second data table. 
	 */
	public void setSecondTableDataModel(DataModel secondTableDataModel) {
		this.secondTableDataModel = secondTableDataModel;
	}
	
	/**
	 * Setter for the personalCases html checkbox place-holder.
	 * 
	 * @param personalCases the html checkbox placeholder to set to personal
	 * cases.
	 */
	public void setPersonalCases(HtmlSelectBooleanCheckbox pCases) {
		personalCases = pCases;
	}
	
	/**
	 * Setter for the displayTable String.
	 * 
	 * @param dTable the internal variable name for the table
	 * being displayed.
	 */
	public void setDisplayTable(String dTable) {
		displayTable = dTable;
	}
	
	/**
	 * Setter for the columnOrder flag.
	 * 
	 * @param order a flag indicating by which column the data table should
	 * be ordered by.
	 */
	public void setColumnOrder(String order) {
		columnOrder = order;
	}
	
	/**
	 * Setter for the listOrdering flag.
	 * 
	 * @param list a flag indicating if the radio button is set for
	 * in ascending or descending order.
	 */
	public void setListOrdering(String list) {
		listOrdering = list;
	}
	
	/**
	 * Setter for the statusCode1_1 integer.
	 * 
	 * @param sc1 a number to be passed to the SQL query for the main
	 * dynamic data table.
	 */
	public void setStatusCode1_1(int sc1) {
		statusCode1_1 = sc1;
	}
	
	/**
	 * Setter for the statusCode1_2 integer.
	 * 
	 * @param sc2 a number to be passed to the SQL query for the main 
	 * dynamic data table.
	 */
	public void setStatusCode1_2(int sc2) {
		statusCode1_2 = sc2;
	}
	
	/**
	 * Setter for the statusCode1_3 integer.
	 * 
	 * @param sc3 a number to be passed to the SQL query for the main dynamic
	 * data table.
	 */
	public void setStatusCode1_3(int sc3) {
		statusCode1_3 = sc3;
	}
	
	/**
	 * Setter for the statusCode1_4 integer.
	 * 
	 * @param sc4 a number to be passed to the SQL query for the main 
	 * dynamic data table.
	 */
	public void setStatusCode1_4(int sc4) {
		statusCode1_4 = sc4;
	}
	
	/**
	 * Setter for the statusCode2_1 integer.
	 * 
	 * @param sc1 a number to be passed to the SQL query for the second
	 * dynamic data table.
	 */
	public void setStatusCode2_1(int sc1) {
		statusCode2_1 = sc1;
	}
	
	/**
	 * Setter for the statusCode2_2 integer.
	 * 
	 * @param sc2 a number to be passed to the SQL query for the second 
	 * dynamic data table.
	 */
	public void setStatusCode2_2(int sc2) {
		statusCode2_2 = sc2;
	}
	
	/**
	 * Setter for the statusCode2_3 integer.
	 * 
	 * @param sc3 a number to be passed to the SQL query for the second
	 * dynamic data table.
	 */
	public void setStatusCode2_3(int sc3) {
		statusCode2_3 = sc3;
	}
	
	/**
	 * Setter for the statusCode2_4 integer.
	 * 
	 * @param sc4 a number to be passed to the SQL query for the second 
	 * dynamic data table.
	 */
	public void setStatusCode2_4(int sc4) {
		statusCode2_4 = sc4;
	}
	
	/**
	 * Setter for the filingsRowCount integer.
	 * 
	 * @param filingsRowCount the number of rows returned by the SQL query that
	 * fills the data table.
	 */
	public void setFilingsRowCount(int filingsRowCount) {
		this.filingsRowCount = filingsRowCount;
	}
	
	/**
	 * Setter for the desc flag.
	 * 
	 * @param d a flag indicating if the sorted list should be placed in
	 * descending order.
	 */
	public void setDesc(boolean d) {
		desc = d;
	}
	
	/**
	 * Setter for the orderByInstantiation flag.  Also, the method sets the
	 * inverted value to the orderByLastEvent flag.
	 * 
	 * @param orderByInst a flag indicating if the table should be ordered
	 * by the 'Initiated Date' column.
	 */
	public void setOrderByInstantiation(boolean orderByInst) {
		orderByInstantiation = orderByInst;
		orderByLastEvent = !orderByInstantiation;
	}
	
	/**
	 * Setter for the orderByLastEvent flag.  Also, the method sets the
	 * inverted value to the orderByInstantiation flag.
	 * 
	 * @param orderByLastEvt a flag indicating if the table should be ordered
	 * by the 'Last Event Time' column.
	 */
	public void setOrderByLastEvent(boolean orderByLastEvt) {
		orderByLastEvent = orderByLastEvt;
		orderByInstantiation = !orderByLastEvent;
	}
	
	/**
	 * Setter for the personalFilter flag.
	 * 
	 * @param pFilter the flag value to set to the personal filter.
	 */
	public void setPersonalFilter(String pFilter) {
		personalFilter = pFilter;
	}
	
	/**
	 * Setter for the currentTableName String.
	 * 
	 * @param name the name of the current table being displayed.
	 */
	public void setCurrentTableName(String name) {
		currentTableName = name;
	}
	
	/**
	 * Setter for the sessionMap map.
	 * 
	 * @param sessionMap the Map to replace the current session map.
	 */
	@SuppressWarnings("unchecked")
	public void setSessionMap(Map sessionMap) {
		this.sessionMap = sessionMap;
	}
	
	/**
	 * Setter for the displaySecondTable flag.
	 * 
	 * @param displaySecondTable
	 */
	public void setDisplaySecondTable(boolean display) {
		displaySecondTable = display;
	}
	
	/**
	 * Setter for the filing object.
	 * 
	 * @param filing the filing currently being worked on.
	 */
	public void setFiling(Filing filing) {
		this.filing = filing;
	}
	
	/**
	 * Setter for the transmissionErrorCount integer.
	 * 
	 * @param transmissionErrorCount the number of transmission errors.
	 */
	public void setTransmissionErrorCount(int transmissionErrorCount) {
		this.transmissionErrorCount = transmissionErrorCount;
	}
	
	/**
	 * Setter for the caseDetailsPageClicked UIParamater.
	 * 
	 * @param caseDetailsPageClicked the UIParameter that is bound to the case
	 * details link.
	 */
	public void setCaseDetailsPageClicked(UIParameter caseDetailsPageClicked) {
		this.caseDetailsPageClicked = caseDetailsPageClicked;
	}
	
	/**
	 * Default Constructor.
	 */
	public MainPage() {
		
		courtUuid = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
		
		/* If no table has been selected, set the value of displayTable to 
		 * "newFilings" to prevent null pointer exceptions. */
		if (displayTable == null) {
			displayTable = "";
		}
		
		/* Helper classes to ensure proper loading.  See their javadoc for more
		 * information. */
		chooseCurrentTableName();
		switchTable();
		tableSortRadioHandler();
		decideToFilterPersonalCases();
		decideShowSecondTable();
		setSecondTable();
	}
	
	/**
	 * Sets the values for the second table when Child Enforcement Main is
	 * chosen.
	 */
	public void setSecondTable() {

		setStatusCode2_1(17);
		setStatusCode2_2(17);
		setStatusCode2_3(17);
		setStatusCode2_4(17);
	}
	
	/**
	 * Decides whether or not to display the second table, based on if the
	 * child enforcement option is picked.
	 * 
	 * @return display the boolean which decides if the second table is to
	 * be rendered.
	 */
	public void decideShowSecondTable() {
		
		boolean display = false;
		
		if (displayTable.equalsIgnoreCase("dispositionReview")) {
			display = true;
		}
		
		setDisplaySecondTable(display);
	}
	
	/**
	 *  Sets the proper displayed table name for the current data table.
	 *  Defaults to displaying "No table" in case a malformed code is
	 *  passed.
	 */
	public String chooseCurrentTableName() {
		
		if (displayTable.equalsIgnoreCase("dispositionReview")) {
			setCurrentTableName("Cases Ready For Disposition Review");
		}
		else if (displayTable.equalsIgnoreCase("allOfficialFilings")) {
			setCurrentTableName("All Official Filings");
		}
		else if (displayTable.equalsIgnoreCase("childEnforcement")) {
			setCurrentTableName("Child Enforcement Filings");
		}
		else if (displayTable.equalsIgnoreCase("readyToImport")) {
			setCurrentTableName("Ready To Import Filings");
		}
		else if (displayTable.equalsIgnoreCase("received")) {
			setCurrentTableName("All Received Filings");
		}
		else if (displayTable.equalsIgnoreCase("imported")) {
			setCurrentTableName("Imported Filings");
		}
		else if (displayTable.equalsIgnoreCase("rejected")) {
			setCurrentTableName("Rejected Filings");
		}
		else if (displayTable.equalsIgnoreCase("served")) {
			setCurrentTableName("Served Filings");
		}
		else if (displayTable.equalsIgnoreCase("nonServed")) {
			setCurrentTableName("Non-Served Filings");
		}
		else if (displayTable.equalsIgnoreCase("disposed")) {
			setCurrentTableName("Cases Decided");
		}
		else if (displayTable.equalsIgnoreCase("appellate")) {
			setCurrentTableName("Appellate Cases");
		}
		else if (displayTable.equalsIgnoreCase("civil")) {
			setCurrentTableName("Civil Cases");
		}
		else {
			setCurrentTableName("No Table");
		}
		
		return currentTableName;
	}
	
	/**
	 * Sets the SQL query to get the proper data table.  Defaults to new filing
	 * table.  Also, refreshes the data model.
	 */
	public void switchTable() {
		
			if (displayTable.equalsIgnoreCase("dispositionReview")) {
				setStatusCode1_1(13);
				setStatusCode1_2(13);
				setStatusCode1_3(13);
				setStatusCode1_4(13);
			}
			else if (displayTable.equalsIgnoreCase("allOfficialFilings")) {
				setStatusCode1_1(2);
				setStatusCode1_2(2);
				setStatusCode1_3(2);
				setStatusCode1_4(2);
			}
			else if (displayTable.equalsIgnoreCase("childEnforcement")) {
				setStatusCode1_1(1);
				setStatusCode1_2(1);
				setStatusCode1_3(1);
				setStatusCode1_4(1);
			}
			else if (displayTable.equalsIgnoreCase("received")) {
				setStatusCode1_1(1);
				setStatusCode1_2(18);
				setStatusCode1_3(20);
				setStatusCode1_4(20);
			}
			else if (displayTable.equalsIgnoreCase("imported")){
				setStatusCode1_1(2);
				setStatusCode1_2(2);
				setStatusCode1_3(2);
				setStatusCode1_4(2);
			}
			else if (displayTable.equalsIgnoreCase("rejected")){
				setStatusCode1_1(3);
				setStatusCode1_2(3);
				setStatusCode1_3(3);
				setStatusCode1_4(3);
			}
			else if (displayTable.equalsIgnoreCase("served")){
				setStatusCode1_1(4);
				setStatusCode1_2(4);
				setStatusCode1_3(4);
				setStatusCode1_4(4);
			}
			else if (displayTable.equalsIgnoreCase("nonServed")){
				setStatusCode1_1(5);
				setStatusCode1_2(5);
				setStatusCode1_3(5);
				setStatusCode1_4(5);
			}
			else if (displayTable.equalsIgnoreCase("disposed")){
				setStatusCode1_1(6);
				setStatusCode1_2(6);
				setStatusCode1_3(6);
				setStatusCode1_4(6);
			}
			else if (displayTable.equalsIgnoreCase("appellate")){
				setStatusCode1_1(1);
				setStatusCode1_2(1);
				setStatusCode1_3(1);
				setStatusCode1_4(1);
			}
			else if (displayTable.equalsIgnoreCase("civil")) {
				setStatusCode1_1(1);
				setStatusCode1_2(1);
				setStatusCode1_3(1);
				setStatusCode1_4(1);
			}
			else {
				setDisplayTable("dispositionReview");
				setStatusCode1_1(13);
				setStatusCode1_2(13);
				setStatusCode1_3(13);
				setStatusCode1_4(13);
			}
			
	}
	
	public void tableSortRadioHandler() {
		
		/* Chooses which column to order the table by. Defaults to
		 * Last Event Time. */
		if(columnOrder != null) {
			if (columnOrder.equalsIgnoreCase("init")) {
				changeOrderToInstantiation();
			}
			else if (columnOrder.equalsIgnoreCase("evt")) {
				changeOrderToEvent();
			}
			else {
				changeOrderToEvent();
			}
		}
		else {
			changeOrderToEvent();
		}
		
		/* Chooses whether to order the sorted column in ascending
		 * or descending order.  Defaults to descending. */
		if (listOrdering != null) {
			if(listOrdering.equalsIgnoreCase("asc")) {
				setDesc(false);
			}
			else if(listOrdering.equalsIgnoreCase("desc")) {
				setDesc(true);
			}
			else {
				setDesc(true);
			}
		}
		else {
			setDesc(true);
		}
	}
	
	/**
	 * Sets the variable to make the SQL query order the list by instantiation
	 * date.
	 */
	public void changeOrderToInstantiation() {
		this.setOrderByInstantiation(true);
	}
	
	/**
	 * Sets the variable to make the SQL query order the list by last event
	 * date.
	 */
	public void changeOrderToEvent() {
		this.setOrderByLastEvent(true);
	}
	
	/**
	 * Sets the personalFilter String to either 'null' if the personalCasese
	 * checkbox is not selected, or changes the personalFilter String to the
	 * user's uuid, to be used in the SQL query.
	 */
	public void decideToFilterPersonalCases() {
		if(personalCases == null) {
		}		
		else if(personalCases.isSelected()) {
			Person person = (Person)this.getSessionMap().get("person");
			personalFilter = (String)person.getUuid();
		}
		else {
			personalFilter = "null";
		}
	}
	
	/**
	 * Internal Class used for paging of Imported Status Filings Data
	 * 
	 * @author mcelratr
	 */
	@SuppressWarnings("unchecked")
	private class MainTableDataModel extends PagedListDataModel {
		
		public MainTableDataModel(int pageSize) {
            super(pageSize);
        }
        
        public DataPage<Filing> fetchPage(int startRow, int pageSize) {
            // call enclosing managed bean method to fetch the data
        	return MainPage.this.getMainTableData(startRow, pageSize);
        }
    }
	
	/**
	 * Internal Class used for paging of Imported Status Filings Data
	 * 
	 * @author mcelratr
	 */
	@SuppressWarnings("unchecked")
	private class SecondaryTableDataModel extends PagedListDataModel {
		
		public SecondaryTableDataModel(int pageSize) {
            super(pageSize);
        }
        
        public DataPage<Filing> fetchPage(int startRow, int pageSize) {
            // call enclosing managed bean method to fetch the data
        	return MainPage.this.getSecondTableData(startRow, pageSize);
        }
    }
	
	/**
	 * Pages large data sets with Data Page class.
	 */
	private DataPage<Filing> getMainTableData(int startRow,
			int pageSize) {
		int dataSetSize = getFilingsRowCount();
		DataPage<Filing> receivedDataTableListForPage = null;
		
		if(displayTable.equalsIgnoreCase("civil")) {
			List<Filing> rFilings = this.getModelFacade()
				.findCaseFilingsByType(2, courtUuid);
			receivedDataTableListForPage = 
				new DataPage<Filing>(dataSetSize, startRow, rFilings);
		}
		else if(displayTable.equalsIgnoreCase("appellate")) {
			List<Filing> rFilings = this.getModelFacade()
				.findCaseFilingsByType(3, courtUuid);
			receivedDataTableListForPage = 
				new DataPage<Filing>(dataSetSize, startRow, rFilings);				
		}
		else if (displayTable.equalsIgnoreCase("childEnforcement")) {
			List<Filing> rFilings = this.getModelFacade()
				.findAllFilingsByTypeForJudge(1, courtUuid);
			receivedDataTableListForPage = 
				new DataPage<Filing>(dataSetSize, startRow, rFilings);
		}
		else {
			List<Filing> rFilings = this.getModelFacade().sortFilingPaged
				(1, getStatusCode1_1(), getStatusCode1_2(),
					getStatusCode1_3(), getStatusCode1_4(), getDesc(),
					getOrderByLastEvent(), startRow, getPersonalFilter(),
					pageSize, courtUuid);
			receivedDataTableListForPage = 
				new DataPage<Filing>(dataSetSize, startRow, rFilings);
		}
		return receivedDataTableListForPage;
	}
	
	/**
	 * Pages large data sets with Data Page class.
	 */
	private DataPage<Filing> getSecondTableData(int startRow,
			int pageSize) {
		int dataSetSize = getSecondTableRowCount();
		DataPage<Filing> receivedDataTableListForPage = null;
				List<Filing> rFilings = this.getModelFacade().sortFilingPaged
					(1, getStatusCode2_1(), getStatusCode2_2(),
						getStatusCode2_3(), getStatusCode2_4(), getDesc(),
						getOrderByLastEvent(), startRow, getPersonalFilter(),
						pageSize, courtUuid);
				receivedDataTableListForPage = 
					new DataPage<Filing>(dataSetSize, startRow, rFilings);
				
			return receivedDataTableListForPage;
	}

	/* ********** *
	 * Navigation *
	 * ********** */
	
	/**
	 * This method navigates user to the Transmission Error Filing Page
	 * 
	 * @return String the navigation string to the transmission error page.
	 */
	public String gotoTransmissionErrorFilingPage(){		 
		return "transmissionErrorFilingPage";		 
	}
	
	/**
	 * This method navigates user to the Case Details Page
	 * 
	 * @return String the navigation string to the case details page.
	 */
	@SuppressWarnings("unchecked")
	public String gotoCaseDetailsPage(){

		Map sessionMap = this.getSessionMap();
		
		Filing clickedJudgeFiling = (Filing)this.getCaseDetailsPageClicked()
			.getValue();
		
		try {
			clickedJudgeFiling.getUuid();
		} catch (Exception e) {
			clickedJudgeFiling = (Filing)this.getCaseDetailsPageClicked2()
				.getValue();
		}
		
		this.setFiling(clickedJudgeFiling);
		
	 	sessionMap.put("caseUuid", filing.getUuid());	 	
	 	sessionMap.put("from", "judgeMainPage");
	 	
	 	return "gotoCaseDetailsPage";
	}
	
	/**
	 * This method navigates user to search page.
	 * 
	 * @return String the navigation string that takes the user to the search page.
	 */
	@SuppressWarnings("unchecked")
	public String gotoSearchPage(){
		Map sessionMap = this.getSessionMap();
	 	sessionMap.put("from", "judgeMainPage");
		return "gotoSearchPage";
	}
	
	/**
	 * Navigates the user to the reports page.
	 * 
	 * @return String the navigation string that takes the user to the reports page.
	 */
	@SuppressWarnings("unchecked")
	public String gotoReportsPage() {
		Map sessionMap = this.getSessionMap();
	 	sessionMap.put("from", "judgeMainPage");
		return "gotoReportsPage";
	}
	
	/**
	 * This method navigates user to the history page.
	 * 
	 * @return String the string that navigates the user to the case history page.
	 */
	@SuppressWarnings("unchecked")
	public String gotoHistoryPage() {
		Map sessionMap = this.getSessionMap();
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = (String)requestMap.get("caseUuid");
		
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "MainPage");
		
		return "gotoCaseHistoryPage";
	}
}
